import os , asyncio , time


root_path = os.path.abspath(__file__)
for i in range(2):
    root_path = os.path.dirname(root_path)
    print(root_path)

outputsDir = os.path.join(root_path , 'outputs')

# 文件超时时间，一个小时
timeout = 3600
# 每分钟扫描一次
scantime = 60
def clearnOutdated():
    ''' 清除过时的文件 '''
    while True:
        time.sleep(scantime)
        print("###### clearnOutdated")
        for fn in os.listdir(outputsDir) :
            print(fn)
            if 'test' in fn :
                filepath = os.path.join(outputsDir, fn)
                createtime = os.path.getctime(filepath)
                now = time.time()
                d = now - createtime
                if d > timeout :
                    os.remove(filepath)
                    print(f'删除过时文件 {filepath}')

# if __name__ == '__main__':
#     asyncio.run(clearnOutdated())